package com.hnichr.ump.business.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.hnichr.ump.business.Constant;
import com.hnichr.ump.common.doc.entity.Banner;
import com.hnichr.ump.common.doc.entity.Goods;
import com.hnichr.ump.common.doc.service.BannerService;
import com.hnichr.ump.common.doc.service.GoodsService;
import com.mxpioframework.filestorage.entity.MxpioFileInfo;
import com.mxpioframework.filestorage.service.FileStorageService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {
	@Autowired
	private FileStorageService fileStorageService;
	@Autowired
	private BannerService bannerService;

	@Override
	@Transactional(readOnly = false)
	public MxpioFileInfo upload(MultipartFile file) throws Exception {
		return fileStorageService.put(file, file.getOriginalFilename());
	}

	@Override
	@Transactional(readOnly = true)
	public String banner(String id) {
		Criteria criteria = Criteria.create();
		criteria.addCriterion("goodsId", Operator.EQ, id);
		List<Banner> banners = bannerService.list(Banner.class, criteria);
		if (CollectionUtil.isNotEmpty(banners)) {
			return CollectionUtil.getFirst(banners).getPic();
		}
		return StrUtil.EMPTY;
	}

	@Override
	@Transactional(readOnly = false)
	public void refresh(Map<String, Object> param) {
		boolean isBanner = Boolean.valueOf(MapUtil.getStr(param, "banner"));
		String goodsId = MapUtil.getStr(param, "goodsId");
		String pic = MapUtil.getStr(param, "pic");
		// 获取现有配置
		Criteria criteria = Criteria.create();
		criteria.addCriterion("goodsId", Operator.EQ, goodsId);
		List<Banner> banners = bannerService.list(Banner.class, criteria);
		Banner banner = CollectionUtil.getFirst(banners);
		if (isBanner) {
			// 需要首页显示
			if (ObjectUtil.isNull(banner)) {
				// 没有配置，保存一条
				banner = new Banner();
				Goods goods = this.getById(Goods.class, goodsId);
				banner.setTitle(goods.getGoodsName());
				banner.setGoodsId(goodsId);
				banner.setPic(pic);
				banner.setSeqNo(10);
				banner.setValidFlag(Constant.STATUS_VALID);
				bannerService.save(banner);
			} else {
				banner.setPic(pic);
				bannerService.update(banner);
			}
		} else {
			// 不需要显示
			if (ObjectUtil.isNotNull(banner)) {
				bannerService.remove(Banner.class, banner.getId());
			}
		}
	}

	@Override
	@Transactional(readOnly = true)
	public List<Goods> search(String key) {
		if (StrUtil.isBlank(key)) {
			return ListUtil.empty();
		}
		List<Goods> goods = JpaUtil.linq(Goods.class).and().equal("validFlag", Constant.STATUS_VALID)
				.equal("onlineFlag", "10").end().or().like("goodsName", key).equal("goods", key).list();
		return goods;
	}

}
